package com.mes.framework.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;
import com.mes.common.utils.SecurityUtils;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

/**
 * Mybatis支持*匹配扫描包
 *
 * @author ruoyi
 */
@Configuration
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
public class MyBatisConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 多租户插件
        interceptor.addInnerInterceptor(tenantLineInnerInterceptor());

        return interceptor;
    }




    /**
     * TenantLineInnerInterceptor 多租户插件
     * https://baomidou.com/guide/interceptor-tenant-line.html
     * DynamicTableNameInnerInterceptor 动态表名插件
     * https://baomidou.com/guide/interceptor-dynamic-table-name.html
     */

    public TenantLineInnerInterceptor tenantLineInnerInterceptor() {

        return new TenantLineInnerInterceptor(new TenantLineHandler() {
            @Override
            public Expression getTenantId() {
                if (SecurityUtils.getTenantId() != null) {
                    //多租户模式，租户id从当前用户获取
                    return new StringValue(SecurityUtils.getTenantId());
                } else {
                    //多租户模式，租户id为null
                    return new StringValue();
                }

            }

            @Override
            public String getTenantIdColumn() {
                // 对应数据库租户ID的列名
                return "tenant_id";
            }

            // 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件
            @Override
            public boolean ignoreTable(String tableName) {
                String loginUser = String.valueOf(SecurityUtils.getAuthentication());
                // 判断是否登录，如果登录则过滤
                if (loginUser.contains("LoginUser")) {
                    // 判断是否平台超级管理员，如果是平台超级管理员则拥有所有数据权限
                  //  if (!SecurityUtils.isAdmin()) {
                        // 需要过滤租户的表
                        List<String> tableNameList = Arrays.asList("sys_job","sys_job_log","sys_user_role","sys_menu",
                                "sys_tenant","sys_dict_data","sys_dict_type","sys_config","sys_role_menu","gen_table",
                                "gen_config_template","sys_role_dept","sys_user_post","view_tables","gen_table_column","view_columns",
                                "sys_tenant_init_menu");
                        return tableNameList.contains(tableName);
                 //   }
                }
                return true;
            }
        });
    }
}
